﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Castle.DynamicProxy;
using OOP.AOP.lib2.Attr;

namespace OOP.AOP.lib2.Aspect
{
    public class LoggingInterceptor : IInterceptor
    {
        public void Intercept(IInvocation invocation)
        {
            var logAttr = invocation.Method.GetCustomAttribute<LogAttribute>();
            if (logAttr != null)
            {
                Console.WriteLine($"[LOG] Start: {invocation.Method.Name} {logAttr.Message}");
            }

            var sw = Stopwatch.StartNew();
            invocation.Proceed();
            sw.Stop();

            if (logAttr != null)
            {
                Console.WriteLine($"[LOG] End: {invocation.Method.Name}, Duration: {sw.ElapsedMilliseconds}ms");
            }
        }
    }

}
